import java.io.*;
import java.util.HashMap;

/**
 * 
 */

/**
 * @author dave
 *
 */
public class DateSkipListMaker {
	
	public static void main(String[] args){
		demo(args[0]);
	}
	
	public static void demo(String data){
		StringSkiplist list = makeSkipList(data);
		System.out.println(list);
		int choice =-1;
		while(choice != 0){
			Terminal.println("Options: Word Search(1), Range Search(2), Remove(3), Print List(4), Contains Key(5), Exit(0): ");
			choice = Terminal.readInt();
			if(choice == 1){
				Terminal.println("Word: ");
				String word = Terminal.readWord();
				Terminal.println(list.wordSearch(word));
			}
			if(choice == 2){
				Terminal.println("Start: ");
				int start = Terminal.readInt();
				Terminal.println("Finish: ");
				int finish = Terminal.readInt();
				Terminal.println(list.rangeSearch(start,finish));
			}
			if(choice == 3){
				Terminal.println("Date: ");
				int date = Terminal.readInt();
				Terminal.println(list.remove(date));
			}
			if(choice == 4){
				Terminal.println(list.toString());
			}
			if(choice == 5){
				Terminal.println("Date: ");
				int date = Terminal.readInt();
				Terminal.println(""+list.containsKey(date));
			}
			
		}
		
	}
	
	public static StringSkiplist makeSkipList(String filename){
		StringSkiplist result = new StringSkiplist();
		
		BufferedReader r;
		// Create a reader for the file
		//
		try {
			InputStream is = new FileInputStream(filename);
			r = new BufferedReader(new InputStreamReader(is));
		}
		catch (IOException e) {
			System.out.println("IOException while opening " + e);
			return result;
		}
			
		// Parse each line of the file sequentially
		//
		try {
			boolean stop = false;
			while (!stop){
				String nextline = r.readLine();
				if (nextline == null) // end of file
					stop = true;
				else {
					int tabLoc = nextline.indexOf('\t');
					String tempDate = nextline.substring(0,tabLoc);
					int date = Integer.parseInt(tempDate);
					String description = nextline.substring(tabLoc+1,nextline.length());        
					
					result.put(date, description);
				}
			}
		}
		catch (IOException e) {
			System.out.println("IOException while reading" + e);
		}
		return result;
	}
}
